package com.sgb.member.view_model;

import androidx.annotation.NonNull;

import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.entity.ListMResponse;
import com.sgb.member.entity.MemberAssetInfoListBean;
import com.sgb.member.ui.fragment.FixedAssetsFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by：冯齐 on 2021/1/26 18:06
 * Description：
 */
public class FixedAssetsModel extends BaseViewModel {

    FixedAssetsFragment fragment;
    MemberRepertory repertory;
    private int current;

    public FixedAssetsModel(FixedAssetsFragment fragment, MemberRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    //固定资产列表
    public void getMemberAssetInfoList(boolean isLoadMore,String companyName, String assetTypeJp) {
        int currentPage = isLoadMore ? current + 1 : 1;
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map<String, Object> map = new HashMap<>();
        map.put("companyName", companyName);
        map.put("limit", 20);
        map.put("page", currentPage);
        map.put("assetTypeJp", assetTypeJp);
        repertory.getApi().getMemberAssetInfoList(map).enqueue(new ICallBack<BaseEntity<ListMResponse<List<MemberAssetInfoListBean>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListMResponse<List<MemberAssetInfoListBean>>> data) {
                current = currentPage;
                int total = data.getData().getPages();
                fragment.setMemberAssetInfoList(data.getData().getList(), isLoadMore, current >= total);
            }

            @Override
            public void onFailure(Call<BaseEntity<ListMResponse<List<MemberAssetInfoListBean>>>> call, Throwable t) {
                super.onFailure(call, t);
                fragment.showEmpty();
            }
        });
    }

    //资质类型查询
    public void getgetAssetType() {
        DialogHelper.showProgressDialog(fragment.getActivity(), "数据获取中...").setCanceledOnTouchOutside(false);
        repertory.getApi().getAssetType().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                fragment.setQualifyTypeTreeList(data.getData());
            }
        });
    }
}
